home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1998 / MacHack 1998.toast / Sessions / Completions / Completions Source / Tasks / TaskLife.cp < prev    next >
Encoding:
Text File  |  1998-06-14  |  2.0 KB  |  120 lines  |  [TEXT/CWIE]

  1. // TaskLife.cp
  2.  
  3. #ifndef TaskLife_h
  4. #include "TaskLife.h"
  5. #endif
  6. #ifndef Assert_h
  7. #include "Assert.h"
  8. #endif
  9. #ifndef Task_h
  10. #include "Task.h"
  11. #endif
  12.  
  13. TaskLife::TaskLife()
  14.   : task( 0 ),
  15.      complete( true ),
  16.      runDeferredCompletor( this, &TaskLife::RunCompletor ),
  17.      runApplicationCompletor( this, &TaskLife::RunCompletor ),
  18.      applicationLink( runApplicationCompletor )
  19.   {
  20.     Assert( !DeferredTaskTime::IsNow() );
  21.     ApplicationTaskQueue::The();
  22.   }
  23.  
  24. TaskLife::~TaskLife()
  25.   {
  26.     Assert( !DeferredTaskTime::IsNow() );
  27.     KillAndWait();
  28.     Assert( Complete() );
  29.   }
  30.  
  31. void TaskLife::Launch( Task *task, Completor completor )
  32.   {
  33.     Assert( Complete() );
  34.     Assert( this->task == 0 );
  35.     
  36.     this->completor = completor;
  37.     complete = false;
  38.     this->task = task;
  39.     
  40.     if ( task == 0 )
  41.         QueueCompletor();
  42.      else
  43.       {
  44.         Assert( task->life == 0 );
  45.         task->life = this;
  46.         task->Launch();
  47.       }
  48.   }
  49.  
  50. void TaskLife::QueueCompletor()
  51.   {
  52.     Assert( !Complete() );
  53.     
  54.     if ( completor.WantsApplicationTime() )
  55.         ApplicationTaskQueue::The().Put( applicationLink );
  56.      else
  57.         deferer.Defer( runDeferredCompletor );
  58.   }
  59.  
  60. void TaskLife::FinishTask()
  61.   {
  62.     Assert( !Complete() );
  63.     
  64.     Task *task = this->task;
  65.     this->task = 0;
  66.     
  67.     if ( task != 0 )
  68.       {
  69.         Assert( task->life == 0 );
  70.         task->AtCompletion();
  71.       }
  72.     
  73.     complete = true;
  74.   }
  75.  
  76. void TaskLife::RunCompletor( DeferredTaskTime time )
  77.   {
  78.     Completor completor = this->completor;
  79.     this->completor = Completor();
  80.  
  81.     FinishTask();
  82.     
  83.     if ( completor.WantsDeferredTaskTime() )
  84.         completor( time );
  85.   }
  86.  
  87. void TaskLife::RunCompletor( ApplicationTime time )
  88.   {
  89.     Completor completor = this->completor;
  90.     this->completor = Completor();
  91.  
  92.     FinishTask();
  93.     
  94.     if ( completor.WantsApplicationTime() )
  95.         completor( time );
  96.   }
  97.  
  98. void TaskLife::Kill()
  99.   {
  100.     Task *task = this->task;
  101.     if ( task != 0 )
  102.         task->Kill();
  103.   }
  104.  
  105. void TaskLife::WaitForCompletion() const
  106.   {
  107.     Assert( !DeferredTaskTime::IsNow() );
  108.     
  109.     static ApplicationTaskQueue& queue( ApplicationTaskQueue::The() );
  110.     
  111.     while ( !Complete() )
  112.         queue.ExecuteOne();
  113.   }
  114.  
  115. void TaskLife::KillAndWait()
  116.   {
  117.     Kill();
  118.     WaitForCompletion();
  119.   }
  120.